package cn.xudy.async.controller;

import cn.xudy.async.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by Ulegal on 2017/10/19.
 */
@RestController
public class MainController {

    @Autowired
    MainService mainService;

    /**
     *  无返回值async
     * @return
     */
    @GetMapping(value = "/test")
    public String asyncMethodWithVoidReturnType() throws ExecutionException, InterruptedException {

        /**
         *    多线程
         */
        // 多线程无返回值
//        String info = mainService.asyncMethodWithVoidReturnType();

        // 多线程有返回值
//        mainService.asyncInvokeWithParameter("test");

//        //  多线程Future
//        Future<String> future = mainService.asyncInvokeReturnFuture(100);
//
//        //future 异常捕捉
//        System.out.println("Future延迟3秒："+future.get());



        return "你好";
    }


    /**
     *  读写锁
     */
     @GetMapping(value = "/testLock")
     public String testReadWriteLock(){

         String tg = mainService.getReadLock();

         System.out.println("------------------:"+tg);


          return tg;
    }


    /**
     *    多线程模仿读写锁
     */
      @GetMapping(value = "/asyncLock")
     public String asyncAndLock(){

          mainService.asyncMethodLock();


         return "asyncAndLock";
     }





}














